package managedbean.tourist;

import exception.ExistException;
import util.entityhelper.TouristAccountState;
import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import managedbean.PageRedirectorBean;
import session.stateless.SystemMessagesBean;
import session.stateless.TouristAccountSessionBean;
import util.localization.WebLocalization;

/**
 *
 * @author Swee Zhi
 */

@ManagedBean
@RequestScoped
public class TouristLoginManagedBean implements Serializable {
    
    @EJB
    private TouristAccountSessionBean touristAccountMgr;
    // EXCEPTION
    @EJB
    private SystemMessagesBean systemMessages;
    
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    
    // fields passed from session
    private String preferredLocale;
    private PageRedirectorBean redirect;
    
    
    // other fields from database
    private String username;
    private String password;
//    private TouristAccountState touristAccountState;
    private boolean remember;
    private String r;

    /** Creates a new instance of TouristLoginManagedBean */
    public TouristLoginManagedBean() {
        checkCookie();
        
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        /* pre-load values from session and database */
        // check if already logged in...redirect to tourist view page (home)
        if (ctx.getExternalContext().getSessionMap().containsKey("touristAccountId")) {
            // redirect back to login
            redirect = new PageRedirectorBean();
            redirect.goTouristWelcomePage();
        }
        /* END load values from session and database */   
    }

    public void doLogin(ActionEvent event) {
        try {
            // set language for all MANAGERS for all THROWABLE EXCEPTIONS in backend
            // to use this line everytime language changes
            touristAccountMgr.setSelectedLocale(preferredLocale);
            
            TouristAccountState touristAccountState = null;
            try {
                touristAccountState = touristAccountMgr.getTouristAccountByUsername(username);
            }
            catch (ExistException ex) {
                // EXCEPTION
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getLocalizedMessage(), ""));
            }

            System.out.println("Login :"  + username);
            System.out.println(" Password: "+ password);
            
            if (touristAccountState != null) 
            {
                // check if inactivated
                // TODO: need to change accountState + check if inactivationDate is null then can login
                
                // after login
                if (touristAccountState.getPassword().equals(password))
                {
                    System.out.println("touristAccountState acctid:" + touristAccountState.getId());
                    // Save the userid and password in a cookie
                    Cookie usernameCookie = new Cookie("usernameCookie", username);
//                    Cookie passwordCookie = new Cookie("passwordCookie", password);
                    if (remember == false) {
                        r = "false";
                    } else {
                        r = "true";
                    }
                    Cookie rememberCookie = new Cookie("rememberCookie", r);
                    usernameCookie.setMaxAge(3600);
//                    passwordCookie.setMaxAge(3600);

                    ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).addCookie(usernameCookie);
//                    ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).addCookie(passwordCookie);
                    ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).addCookie(rememberCookie);

                    /* LOCALIZATION */
                    // first thing after set cookie is to save details to session! and do whatever required first                        
                    // now after login, replace preferred language with SAVED LANGUAGE in database
                    preferredLocale = touristAccountState.getTourist().getLocale();
                    // set language for related backend MANAGERS for all THROWABLE EXCEPTIONS from now on in
                    // backend if message is not sent from frontend to backend through SystemMessages
                    // to use this line everytime having possible language changes
                    touristAccountMgr.setSelectedLocale(preferredLocale);
                    /* END LOCALIZATION */

                    // check if session has anything..default values if nothing
                    if (ctx.getExternalContext().getSessionMap().isEmpty()) {
                        // create a session if no session created. get whatever user uses currently
                        ctx.getExternalContext().getSession(true);
                        // saved preferredLocale (from database) to session; value used to set values in constructors
                        localization.putSavedLocaleToSession(preferredLocale);
                        ctx.getExternalContext().getSessionMap().put("touristAccountId", touristAccountState.getId());
                    }
                    else {
                        if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().containsKey("serverPort")) {
                                // if there is a previous session to pass the serverPort, retrieve the server port and destroy then create a new session and place the server port inside.
                                int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
                                String selectedLocale = "";
                                if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().containsKey("selectedLocale")) {
                                    selectedLocale = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedLocale").toString();
                                }
                                // clear previous session
                                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
                                // create a new session and add the server port.
                                FacesContext.getCurrentInstance().getExternalContext().getSession(true);
                                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("serverPort", serverPort);
                                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedLocale", selectedLocale);
                            }
                        // saved preferredLocale (from database) to session; value used to set values in constructors
                        localization.putSavedLocaleToSession(preferredLocale);
                        ctx.getExternalContext().getSessionMap().put("touristAccountId", touristAccountState.getId());
                    }
                    
                    // redirection upon success
                    String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
                    // force normal http
                    int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
                    // the project name /Tourism-war
                    String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
                    // TODO: move to page redirector bean
                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/tourist/TouristViewMyFoodSage.xhtml");
                } 
                else 
                {
                    // EXCEPTION
                    String statusMessage = systemMessages.getSystemMessage("LOGIN_INCORRECT", preferredLocale);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
                }
            }
        }
        catch (IOException ex) {
            // EXCEPTION
            String statusMessage = systemMessages.getSystemMessage("REDIRECT_FAIL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }
    
    
    public void checkCookie() {
        String cookieName = null;
        Cookie cookie[] = ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getCookies();
        if (cookie != null && cookie.length > 0) {
            for (int i = 0; i < cookie.length; i++) {
                cookieName = cookie[i].getName();
                if (cookieName.equals("usernameCookie")) {
                    username = cookie[i].getValue();
//                } else if (cookieName.equals("passwordCookie")) {
//                    password = cookie[i].getValue();
                } else if (cookieName.equals("rememberCookie")) {
                    r = cookie[i].getValue();
                    if (r.equals("false")) {
                        setRemember(false);
                    } else if (r.equals("true")) {
                        setRemember(true);
                    }
                }
            }
        } else {
            System.out.println("Cannot find any cookie");
        }
    }

   public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean getRemember() {
        return remember;
    }

    public void setRemember(boolean remember) {
        this.remember = remember;
    }

    public SystemMessagesBean getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesBean systemMessages) {
        this.systemMessages = systemMessages;
    }

    public TouristAccountSessionBean getTouristAccountMgr() {
        return touristAccountMgr;
    }

    public void setTouristAccountMgr(TouristAccountSessionBean touristAccountMgr) {
        this.touristAccountMgr = touristAccountMgr;
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public String getR() {
        return r;
    }

    public void setR(String r) {
        this.r = r;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }
    
}
